<?php

namespace backend\modules\Community\controllers;

use backend\components\actions\RegionAction;
use common\models\c2\entity\Region;
use Yii;
use common\models\c2\entity\Community;
use common\models\c2\search\CommunitySearch;

use cza\base\components\controllers\backend\ModelController as Controller;
use yii\helpers\Json;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\Response;

/**
 * DefaultController implements the CRUD actions for Community model.
 */
class DefaultController extends Controller
{
    public $modelClass = 'common\models\c2\entity\Community';


    public function actions()
    {
        $actions = parent::actions();
        $actions['get-region'] = [
            'class' => RegionAction::className(),
            'model' => Region::className()
        ];
        return $actions;
    }

    /**
     * Lists all Community models.
     * @return mixed
     */
    public function actionIndex()
    {
        // if (!Yii::$app->user->can('P_SYSTEM')) {
        //     return $this->redirect(['view', 'id' => Yii::$app->user->getCommunityID()]);
        // }
        $searchModel = new CommunitySearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'model' => $this->retrieveModel(),
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single Community model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * create/update a Community model.
     * fit to pajax call
     * @return mixed
     */
    public function actionEdit($id = null)
    {
        $model = $this->retrieveModel($id);

        if ($model->isNewRecord && !Yii::$app->user->can('P_SYSTEM')) {
            return 'NOT AUTH';
        }

        if ($model->load(Yii::$app->request->post())) {
            if ($model->save()) {
                Yii::$app->session->setFlash($model->getMessageName(), [Yii::t('app.c2', 'Saved successful.')]);
            } else {
                Yii::$app->session->setFlash($model->getMessageName(), $model->errors);
            }
        }

        return (Yii::$app->request->isAjax) ? $this->renderAjax('edit', ['model' => $model,]) : $this->render('edit', ['model' => $model,]);
    }

    /**
     * Finds the Community model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Community the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Community::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
